﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Hec.TextMining;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace Tests
{
    [TestClass]
    public class TestFileConversion
    {
        [TestMethod]
        public void Test_Convert_ToMultiple_TextFiles()
        {
            var mySource = @"\Files\RawFiles\TDWI\tdwi.white.papers.library";
            var myDestination = @"\Files";
            var converter = new PdfToTxtConverter(mySource, myDestination);
            converter.ConvertToTextFiles();
        }

        [TestMethod]
        public void Test_Directories()
        {
            var mySource = @"\Files\RawFiles\TDWI\tdwi.white.papers.library";
            var myDestination = @"\Files";
            var converter = new PdfToTxtConverter(mySource, myDestination);
            converter.GetListOfFilesPerDirectory();
        }

        [TestMethod]
        public void Test_Convert_ToSingle_TextFile()
        {
            var mySource = @"\Files\RawFiles\TDWI\tdwi.white.papers.library";
            var myDestination = @"\Files\Single";
            var converter = new PdfToTxtConverter(mySource, myDestination);
            converter.ConvertToSingleTextFileWithCategory();
        }

        [TestMethod]
        public void Test_NumberOfTabs_Shoudlbe_166By2()
        {
            var file = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\Tests\bin\Debug", "") + @"\Files\Single\AllTexts.txt";
            var sr = new StreamReader(file);

            String line = sr.ReadToEnd();

            var result = line.ToCharArray().GroupBy(x => x).ToList();

            sr.Close();
        }

        [TestMethod]
        public void Test_Count_DistinctWords()
        {
            var mySource = @"\2014.01.Text.Mining\TDWI\tdwi.white.papers.library.flat";
            var mining = new TextMining(mySource);

            mining.getDistinctWords();
        }


        [TestMethod]
        public void Test_distint_characters_in_all_texts()
        {
            var mySource = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\Tests\bin\Debug", "") + @"\Files\";
            var mining = new TextMining(mySource);

            var test = mining.getDistinctCharacters();
            
            var result = string.Join("  ", test);
            /*
             
             ­        '  -  –  —        	  
  
  !  "  #  $  %  &  (  )  *  ,  .  /  :  ;  ?  @  [  ]  _  |  ~  ¦  ‘  ’  “  ”  ¢  €  +  <  =  >  »  §  ©  ®  ·  …  •  ‰  0  ½  1  2  3  4  5  6  7  8  9  a  á  ã  b  c  d  e  é  f  g  h  i  í  ï  j  k  l  m  n  o  ó  ö  p  q  r  s  š  t  ™  u  ü  v  w  x  y  z 
              
             
            ­   */
        }

        [TestMethod]
        public void Test_step_1()
        {
            var mySource = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\Tests\bin\Debug", "") + @"\Files\";
            var mining = new TextMining(mySource);

            mining._1_enleverPonctuation();
            mining._2_enleverApostrophes();
            var test = mining.getDistinctCharacters();
            /*ensure characters are ok.*/
            var result = string.Join("  ", test);
            
        }

        [TestMethod]
        public void Test_step_3()
        {
            var mySource = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\Tests\bin\Debug", "") + @"\Files\";
            var mining = new TextMining(mySource);

            mining._1_enleverPonctuation();
            mining._2_enleverApostrophes();
            mining._3_enleverMotsCourts(3);
        }

        [TestMethod]
        public void Test_Tronquer()
        {
            var mySource = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\Tests\bin\Debug", "") + @"\Files\";
            var mining = new TextMining(mySource);

            var initial = mining.getDistinctWords();
            
            mining._1_enleverPonctuation();
            mining._2_enleverApostrophes();
            mining._3_enleverMotsCourts(3);
            mining._4_tronquerMotsLongs(6);
            
            var final = mining.getDistinctWords();
            var result = "initial: "+initial.Count() + " ----final: " +final.Count();
        }

        [TestMethod]
        public void Test_step_distinctWords()
        {
            var mySource = @"\2014.01.Text.Mining\TDWI\tdwi.white.papers.library.flat";
            var mining = new TextMining(mySource);

            mining._1_enleverPonctuation();
            mining._2_enleverApostrophes();
            mining._3_enleverMotsCourts(3);
            mining._4_tronquerMotsLongs(6);
            mining.getDistinctWords();
        }

        [TestMethod]
        public void Test_dotnet_group_by_with_count_wtf()
        {
            var textA = new Hec.TextMining.KeyValuePair<string, List<string>>("textA", new List<string>() { "mot1", "mot1", "mot3", "mot4" });
            var textB = new Hec.TextMining.KeyValuePair<string, List<string>>("textB", new List<string>() { "mot1", "mot5" });
            var textC = new Hec.TextMining.KeyValuePair<string, List<string>>("textC", new List<string>() { "mot1", "mot3", "mot5" });

            var all = new List<Hec.TextMining.KeyValuePair<string, List<string>>>();
            all.Add(textA);
            all.Add(textB);
            all.Add(textC);

            var many = all.Select(x =>
                            {
                                var list = x.Value.Select(y => 
                                            {
                                                var transform = new Hec.TextMining.KeyValuePair<string, string>(x.Key, y);
                                                return transform;
                                            }).ToList();
                                return list;
                            })
                            .SelectMany(x => x)
                            .ToList();

            // la liste des rows
            var rows = many.Select(x => x.Key).Distinct().ToList();
            // la liste des columns
            var columns = many.Select(x => x.Value).Distinct().ToList();


            var vsm = 
                        rows.Select(row=>
                            {
                                var array = columns.Select(column =>
                                                    {
                                                        var count = many
                                                                    .Where(x => x.Key == row)
                                                                    .Where(x => x.Value == column)
                                                                    .Count();

                                                        var keyValue = new Hec.TextMining.KeyValuePair<string, int>(column, count);
                                                        return keyValue;

                                                    }).ToList();
                                
                                var result = new Hec.TextMining.KeyValuePair<string, List<Hec.TextMining.KeyValuePair<string, int>>>(row, array);
                                return result;
                            })
                            .ToList();

        }

        [TestMethod]
        public void Test_Frequency_VectorSpaceModel()
        {
            var mySource = @"\2014.01.Text.Mining\TDWI\tdwi.white.papers.library.flat";
            var mining = new TextMining(mySource);

            mining._1_enleverPonctuation();
            mining._2_enleverApostrophes();
            mining._3_enleverMotsCourts(3);
            mining._4_tronquerMotsLongs(6);
            mining._5_rawFrequency_VectorSpaceModel(-1);
        }

       
        
    }
}
